
/**
 * shop操作
 */
$(function(){
    var shopId=getQueryString('shopId');
    var isEdit=shopId?true:false;
    var initUrl='/shopadmin/getshopinitinfo';
    var registerShopUrl='/shopadmin/registershop';
    var shopInfoUrl='/shopadmin/getshopbyid?shopId='+shopId;
    var editShopUrl='/shopadmin/modifyshop';

    if(!isEdit){
        getShopInitInfo();
    } else {
        getShopInfo(shopId);
    }

    /**
     * 得到shop信息
     * @param shopId
     */
    function getShopInfo(shopId){
        $.getJSON(shopInfoUrl,function(result){
            if(result.success){
                var shop=result.data.shop;
                $('#shop-name').val(shop.shopName).attr('disable','disable');
                $('#shop-address').val(shop.shopAddress);
                $('#shop-phone').val(shop.phone);
                $('#shop-desc').val(shop.shopDesc);
                var shopCategory='<option data-id="'
                    +shop.shopCategory.shopCategoryId+'"selectd>'
                    +shop.shopCategory.shopCategoryName+'</option>';
                var tempAreaHtml='';
                result.data.areaList.map(function(item,index){
                    tempAreaHtml+='<option data-id="'+item.areaId+'">'
                        +item.areaName+'</option>';
                });
                $('#shop-category').html(shopCategory);
                $('#shop-category').attr('disabled','disabled');
                $('#area').html(tempAreaHtml);
                $("#area option[data-id='"+shop.area.areaId+"']").attr("selected","selected");
            }
        });
    }

    /**
     * 获取shopInfo初始化信息
     * result 为返回的字符串
     */
    function getShopInitInfo(){
        $.getJSON(initUrl,function(result){
            if (result.success) {
                var tempHtml = '';
                var tempAreaHtml = '';
                result.data.shopCategoryList.map(function (item, index) {
                    tempHtml += '<option data-id="' + item.shopCategoryId + '">' + item.shopCategoryName + '</option>';
                });
                result.data.areaList.map(function (item, index) {
                    tempAreaHtml += '<option data-id="' + item.areaId + '">' + item.areaName + '</option>';
                });
                $('#shop-category').html(tempHtml);
                $('#area').html(tempAreaHtml);
            }
        });
    }

    /**
     * 提交表单事件
     */
    $('#submit').click(function(){
        var shop={}
        if(isEdit)shop.shopId=shopId;
        shop.shopName=$('#shop-name').val();
        shop.shopAddress=$('#shop-address').val();
        shop.phone=$('#shop-phone').val();
        shop.shopDesc=$('#shop-desc').val();
        shop.shopCategoryId=$('#shop-category').find('option').not(function () {return !this.selected;}).data('id');
        shop.areaId=$('#area').find('option').not(function(){return !this.selected;}).data('id');
        var shopImg=$('#shop-img')[0].files[0];
        var formData=new FormData();
        var verifyCodeActual=$('#j-captcha').val();
        formData.append("shopInfo",JSON.stringify(shop));
        formData.append("verifyCodeActual",verifyCodeActual);
        formData.append("shopImg",shopImg);
        if(!verifyCodeActual){
            $.toast('请输入验证码!');
            return;
        }
        $.ajax({
            url : isEdit?editShopUrl:registerShopUrl,
            type : 'POST',
            data : formData,
            contentType : false,
            processData : false,
            cache : false,
            success : function(data) {
                if (data.success) {
                    $.toast('提交成功！');
                    // window.location.href = '/myo2o/shop/ownerlogin';
                } else {
                    $.toast(data.msg.toString());
                    $('#captcha-img').click();
                }
            }
        });
    });

})
